<?php


class Database {
	
        private $mysqli = NULL;
        
        public function Connect(DBConfig $config) {
                $this->mysqli = new \mysqli($config->m_host, 
                                                         $config->m_user, 
                                                         $config->m_pass, 
                                                         $config->m_db);

                
                if ($this->mysqli->connect_error) {
                    throw new \Exception($this->mysqli->connect_error);
                }
                
                $this->mysqli->set_charset("utf8");
                
                return true;
        }
		
		public function Prepare($sql) {
                $ret = $this->mysqli->prepare($sql);
                
                if ($ret == FALSE) {
                        throw new \Exception($this->mysqli->error);
                }
                
                return $ret;
                
        }
		
		public function ControlUser($stmt){
				
			$stmt -> execute();
            
            $result = $stmt -> get_result();

            while ($row = $result -> fetch_array(MYSQLI_NUM)) {
                        $stmt -> close();
                        return true;
            }
                    
            $stmt -> close();
			
			return false;
			
         }
		
		
		
		public function RunInsertQuery(\mysqli_stmt $stmt){
			
			if($stmt->execute()==FALSE){
				throw new \Exception($this->mysqli->error);
			}
			
			$ret = $stmt->insert_id;
			
			$stmt->close();
			
			return $ret;
		}
		
		public function Delete($sql){
			
			$stmt = $this->Prepare($sql);
			
	        $stmt -> bind_param("s", $username);
	
	        if ($stmt === FALSE) {
	                
	                return false;
	        }
	        $stmt -> execute();
	        
	        $result = $stmt -> get_result();
	        
	        $stmt -> close();
				
			}
		
		
		public function Close() {
                return $this->mysqli->close();
        }
		
       
}
